class ReportTemplatesController < ApplicationController
  before_filter :login_required

  # GET /report_templates
  # GET /report_templates.xml
  def index
    @report_templates = ReportTemplate.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @report_templates }
    end
  end


  # GET /report_templates/new
  # GET /report_templates/new.xml
  def new
    @report_template = ReportTemplate.new(:ticket_template => TicketTemplate.find(:first))
    @ticket_templates = TicketTemplate.find(:all)
    @heading = "New report template"
    @report_template.selected_main_fields = ["name", "deadline", "status", "number", "created_at", "updated_at", "created_by", "updated_by", "group"]


    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @report_template }
    end
  end

  # GET /report_templates/1/edit
  def edit
    @report_template = ReportTemplate.find(params[:id])
    @heading = "Editing report template"
  end

  # POST /report_templates
  # POST /report_templates.xml
  def create
    @report_template = ReportTemplate.new(params[:report_template])
    @report_template.selected_main_fields = [] if !@report_template.selected_main_fields

    respond_to do |format|
      if @report_template.save
        flash[:notice] = 'Report was successfully created.'
        format.html { redirect_to(report_templates_path) }
        format.xml  { render :xml => @report_template, :status => :created, :location => @report_template }
      else
        @ticket_templates = TicketTemplate.find(:all)
        @heading = "New report template"
        format.html { render :action => "new" }
        format.xml  { render :xml => @report_template.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /report_templates/1
  # PUT /report_templates/1.xml
  def update
    @report_template = ReportTemplate.find(params[:id])

    respond_to do |format|
      if @report_template.update_attributes(params[:report_template])
        flash[:notice] = 'Report template was successfully updated.'
        format.html { redirect_to(report_templates_path) }
        format.xml  { head :ok }
      else
        @heading = "Editing report template"
        format.html { render :action => "edit" }
        format.xml  { render :xml => @report_template.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /report_templates/1
  # DELETE /report_templates/1.xml
  def destroy
    @report_template = ReportTemplate.find(params[:id])
    @report_template.destroy

    respond_to do |format|
      format.html { redirect_to(report_templates_url) }
      format.xml  { head :ok }
    end
  end
  
  def update_note_types_fields
    @report_template = ReportTemplate.new(:ticket_template => TicketTemplate.find(params[:report_template][:ticket_template_id]))
    render :partial => "note_types_fields"
  end
  
end
